package com.itheima.restkeeper.strategy.impl;

import com.itheima.restkeeper.CustomerFace;
import com.itheima.restkeeper.constant.TradingConstant;
import com.itheima.restkeeper.enums.OrderEnum;
import com.itheima.restkeeper.exception.ProjectException;
import com.itheima.restkeeper.pojo.Order;
import com.itheima.restkeeper.req.CustomerVo;
import com.itheima.restkeeper.req.OrderVo;
import com.itheima.restkeeper.req.TradingVo;
import com.itheima.restkeeper.service.IOrderService;
import com.itheima.restkeeper.strategy.TradingStrategy;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Version: V1.0
 */
@Component("creditTradingStrategy")
public class CreditTradingStrategy implements TradingStrategy {

    @Autowired
    private IOrderService orderService;

    @DubboReference(version = "${dubbo.application.version}", check = false)
    CustomerFace customerFace;


    @Override
    public TradingVo convertorTradingVo(OrderVo orderVo) {
        Order order = orderService.getById(orderVo.getId());
        CustomerVo customerVo = customerFace.findCustomerByCustomerId(orderVo.getBuyerId());
        //支付渠道
        order.setTradingChannel(orderVo.getTradingChannel());
        //支付类型
        order.setTradingType(orderVo.getTradingType());
        //订单状态：MD
        order.setOrderState(TradingConstant.GZ);
        //收银人id
        order.setCashierId(orderVo.getCashierId());
        //收银人名称
        order.setCashierName(orderVo.getCashierName());
        //结算保存订单信息
        boolean flag = orderService.updateById(order);
        //构建交易单
        if (flag) {
            TradingVo tradingVo = TradingVo.builder()
                    .tradingAmount(order.getPayableAmountSum())
                    .tradingChannel(orderVo.getTradingChannel())
                    .tradingType(orderVo.getTradingType())
                    .tradingState(order.getOrderState())
                    .enterpriseId(orderVo.getEnterpriseId())
                    .storeId(orderVo.getTableId())
                    .payerId(customerVo.getId())
                    .payerName(customerVo.getMobil())
                    .payeeId(orderVo.getCashierId())
                    .payeeName(orderVo.getCashierName())
                    .productOrderNo(orderVo.getOrderNo())
                    .memo(orderVo.getTableName() + ":" + orderVo.getOrderNo())
                    .build();
            return tradingVo;
        } else {
            throw new ProjectException(OrderEnum.UPDATE_FAIL);
        }
    }
}
